/**
 * 
 */
package com.estock.common.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * @author richard
 * 
 */
public class StringUtil {

	protected static final Pattern CSV_QUOTES_PAT = Pattern.compile("^\"|\"$");
	protected static SimpleDateFormat DATE_SIMPLE_FORMAT_1 = new SimpleDateFormat(
			"MM/dd/yyyy");
	protected static SimpleDateFormat DATE_SIMPLE_FORMAT_2 = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static boolean isEmpty(@SuppressWarnings("rawtypes") List list) {
		if (list == null || list.isEmpty())
			return true;
		return false;
	}

	public static boolean isEmpty(String value) {
		return StringUtils.isEmpty(value);
	}

	public static Date parseDate(Object value) {
		Date ret = null;
		try {
			if (value instanceof Date) {
				ret = (Date) value;
			} else if (value instanceof String) {
				String val = CSV_QUOTES_PAT.matcher((String) value)
						.replaceAll("").trim();
				if (val.length() > 0) {
					ret = DATE_SIMPLE_FORMAT_1.parse(val);
				}
			}
		} catch (Exception e) {
			String val = CSV_QUOTES_PAT.matcher((String) value).replaceAll("")
					.trim();
			try {
				ret = DATE_SIMPLE_FORMAT_2.parse(val);
			} catch (Exception e2) {
				throw new RuntimeException(
						"date string cannot match MM/dd/yyyy or yyyy-MM-dd with str=" + val);
			}
		}
		return ret;
	}

	public static Double parseDouble(Object value) {
		Double ret = null;
		try {
			if (value != null) {
				if (value instanceof String) {
					String val = CSV_QUOTES_PAT.matcher((String) value)
							.replaceAll("").trim();
					if (!StringUtils.isEmpty(val)) {
						ret = Double.parseDouble(val);
					}
				} else if (value instanceof Double) {
					ret = (Double) value;
				} else if (value instanceof Integer) {
					ret = ((Integer) value).doubleValue();
				}
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public static Long parseLong(Object value) {
		Long ret = null;
		try {
			if (value != null) {
				if (value instanceof String) {
					String val = CSV_QUOTES_PAT.matcher((String) value)
							.replaceAll("").trim();
					if (!StringUtils.isEmpty(val)) {
						ret = Long.parseLong(val);
					}
				} else if (value instanceof Double) {
					ret = ((Double) value).longValue();
				} else if (value instanceof Integer) {
					ret = ((Integer) value).longValue();
				}
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public static String parseInt2StrWithPadding(String value, int strLength,
			char paddingChar) {
		String format = "%" + strLength + "s";
		return String.format(format, value);
	}

	/**
	  * @param str String to be padded
	  * @param charc padding char
	  * @param minLen smallest length after padded
	  * 
	  * @return String
	  */
	public static String padding(String str, String charc, int minLen) {
		int currentLength = 0;
		if (isEmpty(str)) {
			currentLength = 0;
		} else {
			currentLength = str.length();
		}
		
		if (currentLength > minLen) {
			throw new RuntimeException("current length is larger than minimum, unexpected data!");
		}
		
		StringBuffer result = new StringBuffer("");
		for (int i = 0; i < minLen - currentLength; i++) {
			result.append(charc);
		}
		return result.toString() + str;
	}
	
	/**
	 * 字符格式如果小数不足digit位,会以0补足.
	 * 
	 * @param price
	 * @param digit
	 * @return
	 */
	public static String formatFloat(float price, int digit) {
		
        DecimalFormat decimalFormat = new DecimalFormat("0." + StringUtils.repeat("0", digit));
        return decimalFormat.format(price);
	}
	
	public static void main(String[] args) {
		System.out.println(StringUtil.formatFloat(9.1f, 2));
		System.out.println(StringUtil.formatFloat(9.11f, 2));
		System.out.println(StringUtil.formatFloat(9.2f, 2));
		System.out.println(StringUtil.formatFloat(9, 2));
	}

}
